package it.demosms.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import it.demosms.autoanno.UserLoginToken;
import it.demosms.pojo.dto.LoginUserDTO;
import it.demosms.pojo.dto.RegisterUserDTO;
import it.demosms.service.impl.UserServiceImpl;
import it.demosms.util.Result;
import it.demosms.util.ResultStatusEM;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api("季鑫利:用户controller")
public class UserController {

    /**
     * 日志
     */
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserServiceImpl userService;

    @PostMapping("save")
    @ApiOperation("季鑫利:用户注册")
    public Result saveUser(RegisterUserDTO registerUserDTO, String smsCode) {
        boolean bRet = userService.saveUser(registerUserDTO, smsCode);
        Result result = new Result();
        if (!bRet) {
            result.setStatus(ResultStatusEM.ERROR);
            log.error("【注册失败】");
            return result;
        }
        result.setStatus(ResultStatusEM.OK);
        result.setMessage("注册成功");
        return result;
    }

    @PostMapping(value = "login", produces = "application/json;utf-8")
    @ApiOperation("季鑫利:用户登录")
    public Result login(LoginUserDTO loginUserDTO) {
        return userService.login(loginUserDTO);
    }

    //    @UserLoginToken
    @GetMapping("/getMessage")
    @ApiOperation("季鑫利:token验证")
    public Result getMessage(String token) {
        Result result = new Result();
        boolean bRet = userService.checkToken(token);
        if (!bRet) {
            result.setStatus(ResultStatusEM.ERROR);
            return result;
        }
        result.setStatus(ResultStatusEM.OK);
        return result;
    }

    @GetMapping("getUserIdByToken")
    @ApiOperation("季鑫利:获取token中id")
    public Result getUserIdByToken(String token) {
        Result result = new Result();
        Integer userId = userService.getUserIdByToken(token);
        if(userId<0){
            result.setStatus(ResultStatusEM.ERROR);
            result.setMessage("不存在的id");
            return result;
        }
        result.setStatus(ResultStatusEM.OK);
        result.setData(userId);
        return result;
    }

}

